import type { Resolvers } from '../../generated/graphql';
import {
  addMalwareAnalysis,
  analysisScoPaginated,
  findAll,
  findById,
  hostVmSoftware,
  installedSoftwarePaginated,
  operatingSystemSoftware,
  sampleObservable
} from './malwareAnalysis-domain';
import {
  stixDomainObjectAddRelation,
  stixDomainObjectCleanContext,
  stixDomainObjectDelete,
  stixDomainObjectDeleteRelation,
  stixDomainObjectEditContext,
  stixDomainObjectEditField
} from '../../domain/stixDomainObject';

const malwareAnalysisResolvers: Resolvers = {
  Query: {
    malwareAnalysis: (_, { id }, context) => {
      return findById(context, context.user, id);
    },
    malwareAnalyses: (_, args, context) => {
      return findAll(context, context.user, args);
    },
  },
  MalwareAnalysis: {
    hostVm: (malwareAnalysis, _, context) => hostVmSoftware<any>(context, context.user, malwareAnalysis.id),
    operatingSystem: (malwareAnalysis, _, context) => operatingSystemSoftware<any>(context, context.user, malwareAnalysis.id),
    installedSoftware: (malwareAnalysis, args, context) => installedSoftwarePaginated<any>(context, context.user, malwareAnalysis.id, args),
    analysisSco: (malwareAnalysis, args, context) => analysisScoPaginated<any>(context, context.user, malwareAnalysis.id, args),
    sample: (malwareAnalysis, _, context) => sampleObservable<any>(context, context.user, malwareAnalysis.id),
  },
  Mutation: {
    malwareAnalysisAdd: (_, { input }, context) => {
      return addMalwareAnalysis(context, context.user, input);
    },
    malwareAnalysisDelete: (_, { id }, context) => {
      return stixDomainObjectDelete(context, context.user, id);
    },
    malwareAnalysisFieldPatch: (_, { id, input, commitMessage, references }, context) => {
      return stixDomainObjectEditField(context, context.user, id, input, { commitMessage, references });
    },
    malwareAnalysisContextPatch: (_, { id, input }, context) => {
      return stixDomainObjectEditContext(context, context.user, id, input);
    },
    malwareAnalysisContextClean: (_, { id }, context) => {
      return stixDomainObjectCleanContext(context, context.user, id);
    },
    malwareAnalysisRelationAdd: (_, { id, input }, context) => {
      return stixDomainObjectAddRelation(context, context.user, id, input);
    },
    malwareAnalysisRelationDelete: (_, { id, toId, relationship_type: relationshipType }, context) => {
      return stixDomainObjectDeleteRelation(context, context.user, id, toId, relationshipType);
    },
  }
};

export default malwareAnalysisResolvers;
